package com.xdhl.fu.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.constant.SecurityConstants;
import com.xdhl.fu.common.core.web.controller.BaseController;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.security.annotation.RequiresPermissions;
import com.xdhl.fu.common.security.utils.SecurityUtils;
import com.xdhl.fu.member.api.RemoteMemberBasicService;
import com.xdhl.fu.member.domain.MemberPassenger;
import com.xdhl.fu.member.service.IMemberPassengerService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 会员乘客控制层
 *
 * @author 朱力维
 * @time 2022/5/27 16:40
 */
@RestController
@RequestMapping("/passenger")
public class MemberPassengerController extends BaseController {
    @Resource
    private IMemberPassengerService memberPassengerService;

    @Resource
    private RemoteMemberBasicService remoteMemberBasicService;

    /**
     * 获取会员乘客详细信息
     */
    @RequiresPermissions("member:passenger:query")
    @GetMapping(value = "/{mDataId}")
    public AjaxResult getInfo(@PathVariable("mDataId") String mDataId) {
        LambdaQueryWrapper<MemberPassenger> lqw = new LambdaQueryWrapper<>();
        lqw.eq(true, MemberPassenger::getMemberDataId, mDataId);
        List<MemberPassenger> list = memberPassengerService.list(lqw);
        return AjaxResult.success(list);
    }

    /**
     * 获取登录会员绑定的乘客列表
     */
    @GetMapping("/list")
    public AjaxResult getPassengerList() {
        return AjaxResult.success(memberPassengerService.getPassengerList());
    }

    /**
     * 新增乘客信息
     */
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody MemberPassenger passenger) {
        passenger.setCreateBy(SecurityUtils.getUsername());
        passenger.setCreateTime(LocalDateTime.now());
        passenger.setUpdateBy(passenger.getCreateBy());
        passenger.setUpdateTime(passenger.getCreateTime());
        passenger.setMemberDataId(remoteMemberBasicService.getDataIdSelf(SecurityUtils.getToken(), SecurityConstants.INNER).getData());
        if (memberPassengerService.save(passenger))
            return AjaxResult.success("添加成功");
        return AjaxResult.error("添加失败，请稍后重试");
    }

    /**
     * 删除乘客信息
     */
    @Transactional
    @DeleteMapping("/{dataId}")
    public AjaxResult remove(@PathVariable String dataId) {
        return toAjax(memberPassengerService.removeById(dataId));
    }

    /**
     * 设置默认乘客
     */
    @Transactional
    @PutMapping("/setDefault")
    public AjaxResult edit(@RequestBody MemberPassenger passenger) {
        LambdaQueryWrapper<MemberPassenger> lqw = new LambdaQueryWrapper();
        lqw.eq(MemberPassenger::getMemberDataId, passenger.getMemberDataId());
        List<MemberPassenger> list = memberPassengerService.list(lqw);
        list.forEach(item -> {
            if (item.getPDefault() == 1) {
                item.setPDefault(0);
                memberPassengerService.updateById(item);
            }
        });
        passenger.setPDefault(1);
        return toAjax(memberPassengerService.updateById(passenger));
    }
}
